// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.kit.chatkit.ui.page;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;

import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luoxiang.lib_common.bean.CallEvaluateInfoResult;
import com.luoxiang.lib_common.bean.NumberBean;
import com.luoxiang.lib_common.dialog.EvaluateDialog;
import com.luoxiang.lib_common.utils.AESUtil;
import com.luoxiang.lib_common.utils.LogUtils;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.lib_common.utils.NetUtils;
import com.lxj.xpopup.XPopup;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatActivityBinding;
import com.netease.yunxin.kit.chatkit.ui.view.message.audio.ChatMessageAudioControl;
import com.netease.yunxin.kit.common.ui.activities.BaseActivity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/** BaseActivity for Chat include P2P chat page and Team chat page */
public abstract class ChatBaseActivity extends BaseActivity {

  ChatActivityBinding binding;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ChatActivityBinding.inflate(LayoutInflater.from(this));
    setContentView(binding.getRoot());
    initChat();
    LiveEventBus.<String>get("showEvaluateDialog").observe(this, s -> {
      if(getLifecycle().getCurrentState() == Lifecycle.State.RESUMED){
        getCallEvaluateInfo(s);
      }
    });
  }

  protected abstract void initChat();

  private void getCallEvaluateInfo(String channelId){
    NetUtils.getInstance().getApiService().getEvaluationInfo(MmkvUtil.INSTANCE.decodeString(TOKEN), channelId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<NumberBean>() {
              @Override
              public void onSubscribe(Disposable d) {

              }

              @Override
              public void onNext(NumberBean numberBean) {
                String data = numberBean.getData();
                String decryptStr;
                try {
                  decryptStr = AESUtil.aesDecryptString(data, KEY);
                  LogUtils.i("decryptStrdelete", decryptStr);
                  Gson gson = new Gson();
                  CallEvaluateInfoResult callEvaluateInfoResult = gson.fromJson(decryptStr, CallEvaluateInfoResult.class);
                  if(callEvaluateInfoResult.getData() != null){
                    EvaluateDialog evaluateDialog = new EvaluateDialog(ChatBaseActivity.this);
                    evaluateDialog.setData(channelId,callEvaluateInfoResult.getData());
                    new XPopup.Builder(ChatBaseActivity.this)
                            .asCustom(evaluateDialog)
                            .show();
                  }
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }

              @Override
              public void onError(Throwable e) {

              }

              @Override
              public void onComplete() {

              }
            });
  }

  @Override
  protected void onStop() {
    super.onStop();
    //stop message audio
    ChatMessageAudioControl.getInstance().stopAudio();
  }
}
